<script setup lang="ts">
import { ref } from "vue";
import { useUserStore } from "../pinia/user";
import type { AdminLogin } from "../utils/types";
import type { FormInstance, FormRules } from "element-plus";

const store = useUserStore();
const loginFormRef = ref<FormInstance>();
const loginForm = ref<AdminLogin>({
  adminname: "",
  password: "",
});

const rules = ref<FormRules<typeof loginForm>>({
  adminname: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid:boolean) => {
    if (valid) {
      console.log("submit!");
      store.SET_USER(loginForm.value);
    } else {
      console.log("error submit!");
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<template>
  <div class="login">
    <el-form
      :model="loginForm"
      style="max-width: 350px"
      label-width="auto"
      ref="loginFormRef"
      status-icon
      :rules="rules"
      class="demo-ruleForm">
      <el-form-item label="用户名" prop="adminname">
        <el-input v-model="loginForm.adminname"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(loginFormRef)">登录</el-button>
        <el-button @click="resetForm(loginFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba($color: #316ed0, $alpha: 0.7);
  .el-form {
    padding: 50px 20px;
    background: #fff;
    border-radius: 5px;
    .el-input {
      width: 200px;
    }
  }
}
</style>
